package com.babymall.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.babymall.model.Users;
import com.babymall.service.IUsersService;

@Controller
@RequestMapping("/users")
public class UsersController {
	
	@Resource
	private IUsersService usersService;

	@RequestMapping("/login.do")
	public String login(Users users,HttpSession session) {
		
		Users user = usersService.login(users);
		if(user!=null)
		{
			session.setAttribute("user", user);
			return "index";
		}else {
			return "login";
		}
	}
	
	@RequestMapping("/editPwd.do")
	public String editPwd(String oldPwd,String newPwd,HttpSession session) {
		Users user = (Users)session.getAttribute("user");
		//只有用户登录，才能修改密码
		if(user!=null)
		{
			int n = usersService.editPwd(oldPwd, newPwd,user);
			if(n>0)
			{
				//将会话中的用户密码改成新密码
				user.setUser_password(newPwd);
				session.setAttribute("user", user);
				return "success";
			}else
				return "fail";
		}return "fail";
	}
	
	@ResponseBody
	@RequestMapping("/findAll.do")
	public Map<String,Object> findAll(){
		return usersService.findAll();
	}
}
